package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.databinding.FragmentRecommendNewComicBinding;
import com.qire.manhua.fragment.RecommendNewComicFragment;
import com.qire.manhua.model.viewModel.impl.RecommendNewComicViewModel;
import com.qire.manhua.model.viewModel.impl.RecommendNewComicViewModel.NewComicItemViewModel;
import com.qire.other.recorder.ComicActionRecorder;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class RecommendNewComicController extends EventController<RecommendNewComicFragment> implements AutoVariable {

    private RecommendNewComicViewModel viewModel;

    public RecommendNewComicController(@NonNull RecommendNewComicFragment lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        FragmentRecommendNewComicBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setRecommendNewComicController(this);
        }
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.recommendNewComicController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(RecommendNewComicViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    /**
     * 上拉加载，加载更多数据
     * */
    public void refreshLayoutOnLoadMore(RefreshLayout refreshLayout) {
        viewModel.moreNewComicData();
    }

    /**
     * 下拉刷新，重新获取数据
     * */
    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.loadNewComicData();
        refreshLayout.setNoMoreData(false);
    }

    /**
     * 数据错误，重新加载数据按钮
     * */
    public void errorLayoutOnClick(View view) {
        viewModel.loadNewComicData();
    }

    public void newComicItemOnClick(View view) {
        NewComicItemViewModel itemViewModel = (NewComicItemViewModel) view.getTag();
        if(ObjectUtil.nonNull(itemViewModel)) {
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDetailPage)
                    .withData("comicBookId", itemViewModel.comicId)
                    .navigation();

            // 行为数据统计
            ComicActionRecorder.build(ComicActionRecorder.count_index)
                    .setComicInfo(itemViewModel.comicId, itemViewModel.comicName)
                    .putParam("频道名", "原创")
                    .putParam("图片顺序", String.valueOf(itemViewModel.serialNumber))
                    .doRecorde(view.getContext());
        }
    }


}
